﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BOL;
using BOL.UI;
using UtilityExtensions;
using System.Web.UI.DataVisualization.Charting;
using System.Data.SqlClient;
using Website.Logger;
using System.Web.UI.HtmlControls;
using System.Drawing;
using BOL.Ventas;

namespace UILGestion.VentaAdmin.Chart
{
    public partial class Chart09 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                if (!this.IsPostBack)
                {
                    idProyecto.Value = (string)Request.QueryString["idProyecto"];
                    FormatForm();
                }
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }


        public void FormatForm()
        {

           


        }
        public void LoadChart(int idProyecto, int tipoUI,int  idEtapa)
        {

            var bol = new BOL.Ventas.OrdenVentaBL();

            var lista = bol.GetOrdenVentaPendienteVenta(idProyecto, idEtapa, tipoUI).ToList();
            var separaicion = bol.GetOrdenVentaPendienteSeparacion(idProyecto, idEtapa, tipoUI);
            //lista.Add(separaicion);

                         //cabecera

                                TableRow trsup = new TableRow();
                                trsup.Font.Bold = true;
                                trsup.CssClass = "Heading secTitle AreaHeading ms-crm-List-Header";
                                TableCell td = new TableCell();
                                td.Text = "";
                                td.Width = Unit.Point(70);
                                
                                trsup.Controls.Add(td);
                                
                                td = new TableCell();
                                td.HorizontalAlign = HorizontalAlign.Center;
                                td.Text = "CONCEPTO";
                                td.Width = Unit.Point(120);
                                trsup.Controls.Add(td);

                                td = new TableCell();
                                td.HorizontalAlign = HorizontalAlign.Center;
                                td.Text = "TOTAL";
                                td.Width = Unit.Point(90);
                                trsup.Controls.Add(td);

                                td = new TableCell();
                                td.HorizontalAlign = HorizontalAlign.Center;
                                td.Text = "(+0 -30 DIAS)*";
                                td.Width = Unit.Point(90);
                                trsup.Controls.Add(td);

                                td = new TableCell();
                                td.HorizontalAlign = HorizontalAlign.Center;
                                td.Text = "(+30-60 DIAS)*";
                                td.Width = Unit.Point(90);
                                trsup.Controls.Add(td);

                                td = new TableCell();
                                td.HorizontalAlign = HorizontalAlign.Center;
                                td.Text = "(+ 60 DIAS)*";
                                td.Width = Unit.Point(90);
                                trsup.Controls.Add(td);

                                resumenTBL.Controls.Add(trsup);

                //-------------------------------



                                //------------------------------- Separacion------------------
                                trsup = new TableRow();

                                trsup.BackColor = Color.Linen;
                                trsup.Font.Bold = true;

                                td = new TableCell();
                                td.HorizontalAlign = HorizontalAlign.Center;
                                td.Text = "SEPARACION";
                                trsup.Controls.Add(td);



                                td = new TableCell();
                                td.HorizontalAlign = HorizontalAlign.Right;
                                td.Text = "TOTAL SALDO";
                                trsup.Controls.Add(td);

                                td = new TableCell();
                                td.Text = string.Format("S/. {0:N2}", separaicion.totalXCobrar);
                                td.HorizontalAlign = HorizontalAlign.Right;
                                trsup.Controls.Add(td);


                                td = new TableCell();
                                td.Text = string.Format("S/. {0:N2}", separaicion.hasta30);
                                td.HorizontalAlign = HorizontalAlign.Right;
                                td.ForeColor = separaicion.hasta30 > 0 ? Color.Red : Color.Green;
                                trsup.Controls.Add(td);

                                td = new TableCell();
                                td.Text = string.Format("S/. {0:N2}", separaicion.hasta3060);
                                td.HorizontalAlign = HorizontalAlign.Right;
                                td.ForeColor = separaicion.hasta3060 > 0 ? Color.Red : Color.Green;
                                trsup.Controls.Add(td);

                                td = new TableCell();
                                td.Text = string.Format("S/.{0:N2}", separaicion.hasta60mas);
                                td.HorizontalAlign = HorizontalAlign.Right;
                                td.ForeColor = separaicion.hasta3060 > 0 ? Color.Red : Color.Green;
                                trsup.Controls.Add(td);

                                resumenTBL.Controls.Add(trsup);

                                //-------------------------------

                                trsup = new TableRow();

                                trsup.BackColor = Color.Linen;
                                trsup.Font.Bold = true;
                                td = new TableCell();
                                td.HorizontalAlign = HorizontalAlign.Center;
                                td.Text = "VENTA";
                                trsup.Controls.Add(td);
                                
                                 
                                td = new TableCell();
                                td.HorizontalAlign = HorizontalAlign.Right;
                                td.Text = "TOTAL POR COBRAR";
                                trsup.Controls.Add(td);
                                
                                td = new TableCell();
                                td.Text = string.Format("S/.{0:N2}",lista.Sum(p=>p.totalXCobrar));
                                td.HorizontalAlign = HorizontalAlign.Right; 
                                trsup.Controls.Add(td);


                                td = new TableCell();
                                td.Text = string.Format("S/.{0:N2}", lista.Sum(p => p.hasta30));
                                td.ForeColor = Color.Red;
                                td.HorizontalAlign = HorizontalAlign.Right;
                                trsup.Controls.Add(td);

                                td = new TableCell();
                                td.Text = string.Format("S/.{0:N2}", lista.Sum(p => p.hasta3060));
                                td.HorizontalAlign = HorizontalAlign.Right;
                                td.ForeColor = Color.Red;
                                trsup.Controls.Add(td);

                                td = new TableCell();
                                td.Text = string.Format("S/.{0:N2}", lista.Sum(p => p.hasta60mas));
                                td.HorizontalAlign = HorizontalAlign.Right;
                                td.ForeColor = Color.Red;
                                trsup.Controls.Add(td);

                   resumenTBL.Controls.Add(trsup);
             
                   //-------------------------------

                    trsup = new TableRow();
                               
                                td = new TableCell();
                                td.Font.Bold = true;
                                td.HorizontalAlign = HorizontalAlign.Center;
                                td.Text = "";
                                trsup.Controls.Add(td);
                                
                                 
                                td = new TableCell();
                                td.HorizontalAlign = HorizontalAlign.Right;
                                td.Text = "SALDO CLIENTE";
                                trsup.Controls.Add(td);
                   
                   td = new TableCell();
                   td.Text = string.Format("S/.{0:N2}", lista.Where(o=>o.idconcepto==1).Sum(p => p.totalXCobrar));
                   td.HorizontalAlign = HorizontalAlign.Right;
            
                   trsup.Controls.Add(td);


                   td = new TableCell();
                   td.Text = string.Format("S/.{0:N2}", lista.Where(o => o.idconcepto == 1).Sum(p => p.hasta30));
                   td.HorizontalAlign = HorizontalAlign.Right;
                   td.ForeColor = lista.Where(o => o.idconcepto == 1).Sum(p => p.hasta30) > 0 ? Color.Red : Color.Green; 
                   trsup.Controls.Add(td);

                   td = new TableCell();
                   td.Text = string.Format("S/.{0:N2}", lista.Where(o => o.idconcepto == 1).Sum(p => p.hasta3060));
                   td.ForeColor = lista.Where(o => o.idconcepto == 1).Sum(p => p.hasta3060) > 0 ? Color.Red : Color.Green; 
                   td.HorizontalAlign = HorizontalAlign.Right;
                   trsup.Controls.Add(td);

                   td = new TableCell();
                   td.Text = string.Format("S/.{0:N2}", lista.Where(o => o.idconcepto == 1).Sum(p => p.hasta60mas));
                   td.HorizontalAlign = HorizontalAlign.Right;
                   td.ForeColor = lista.Where(o => o.idconcepto == 1).Sum(p => p.hasta60mas) > 0 ? Color.Red : Color.Green; 
                   trsup.Controls.Add(td);

                   resumenTBL.Controls.Add(trsup);
                   //-------------                
                   //-------------------------------
                   trsup = new TableRow();
                   td = new TableCell();
                   td.Font.Bold = true;
                   td.HorizontalAlign = HorizontalAlign.Center;
                   td.Text = "";
                   trsup.Controls.Add(td);


                   td = new TableCell();
                   td.HorizontalAlign = HorizontalAlign.Right;
                   td.Text = "SALDO BONO";
                   trsup.Controls.Add(td);
                   
                   td = new TableCell();
                   td.Text = string.Format("S/.{0:N2}", lista.Where(o => o.idconcepto == 2).Sum(p => p.totalXCobrar));
                   td.HorizontalAlign = HorizontalAlign.Right;
            
                   trsup.Controls.Add(td);


                   td = new TableCell();
                   td.Text = string.Format("S/.{0:N2}", lista.Where(o => o.idconcepto == 2).Sum(p => p.hasta30));
                   td.HorizontalAlign = HorizontalAlign.Right;
                   td.ForeColor = lista.Where(o => o.idconcepto == 2).Sum(p => p.hasta30) > 0 ? Color.Red : Color.Green; 
                   trsup.Controls.Add(td);

                   td = new TableCell();
                   td.Text = string.Format("S/.{0:N2}", lista.Where(o => o.idconcepto == 2).Sum(p => p.hasta3060));
                   td.HorizontalAlign = HorizontalAlign.Right;
                   td.ForeColor = lista.Where(o => o.idconcepto == 2).Sum(p => p.hasta3060) > 0 ? Color.Red : Color.Green; 
                   trsup.Controls.Add(td);

                   td = new TableCell();
                   td.Text = string.Format("S/.{0:N2}", lista.Where(o => o.idconcepto == 2).Sum(p => p.hasta60mas));
                   td.HorizontalAlign = HorizontalAlign.Right;
                   td.ForeColor = lista.Where(o => o.idconcepto == 2).Sum(p => p.hasta60mas) > 0 ? Color.Red : Color.Green; 
                   trsup.Controls.Add(td);

                   resumenTBL.Controls.Add(trsup);


                   //-------------------------------
                   trsup = new TableRow();
                   td = new TableCell();
                   td.Font.Bold = true;
                   td.HorizontalAlign = HorizontalAlign.Center;
                   td.Text = "";
                   trsup.Controls.Add(td);


                   td = new TableCell();
                   td.HorizontalAlign = HorizontalAlign.Right;
                   td.Text = "SALDO CREDITO HIPOTECARIO";
                   trsup.Controls.Add(td);

                   td = new TableCell();
                   td.Text = string.Format("S/.{0:N2}", lista.Where(o => o.idconcepto == 3).Sum(p => p.totalXCobrar));
                   td.HorizontalAlign = HorizontalAlign.Right;
            
                   trsup.Controls.Add(td);


                   td = new TableCell();
                   td.Text = string.Format("S/.{0:N2}", lista.Where(o => o.idconcepto == 3).Sum(p => p.hasta30));
                   td.HorizontalAlign = HorizontalAlign.Right;
                   td.ForeColor = lista.Where(o => o.idconcepto == 3).Sum(p => p.hasta30) > 0 ? Color.Red : Color.Green; ;
                   
                   trsup.Controls.Add(td);

                   td = new TableCell();
                   td.Text = string.Format("S/.{0:N2}", lista.Where(o => o.idconcepto == 3).Sum(p => p.hasta3060));
                   td.HorizontalAlign = HorizontalAlign.Right;
                   td.ForeColor = lista.Where(o => o.idconcepto == 3).Sum(p => p.hasta3060) > 0 ? Color.Red : Color.Green; ;
                   trsup.Controls.Add(td);

                   td = new TableCell();
                   td.Text = string.Format("S/.{0:N2}", lista.Where(o => o.idconcepto == 3).Sum(p => p.hasta60mas));
                   td.HorizontalAlign = HorizontalAlign.Right;
                   td.ForeColor = lista.Where(o => o.idconcepto == 3).Sum(p => p.hasta60mas) > 0 ? Color.Red : Color.Green;
                   trsup.Controls.Add(td);

                   resumenTBL.Controls.Add(trsup);

            //----------------------

                   

                   resumenTBL.Controls.Add(trsup);
            ////////////////stock

                                 trsup = new TableRow();
                                
                                 td = new TableCell();
                                 td.ColumnSpan = 2;
                                 td.Text = "TOTAL:";
                                 td.HorizontalAlign = HorizontalAlign.Right; 
                                 td.Font.Bold = true;
                                 trsup.Controls.Add(td);

                                 td = new TableCell();
                                 td.Text = string.Format("S/. {0:N2}", lista.Sum(p =>p.totalXCobrar)+separaicion.totalXCobrar);
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.Font.Bold = true;
                                 trsup.Controls.Add(td);

                                 td = new TableCell();
                                 td.Text = string.Format("S/. {0:N2}", lista.Sum(p =>p.hasta30) +separaicion.hasta30);
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.Font.Bold = true;
                                 trsup.Controls.Add(td);

                                 td = new TableCell();
                                 td.Text = string.Format("S/. {0:N2}", lista.Sum(p =>p.hasta3060) +separaicion.hasta3060);
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.Font.Bold = true;
                                 trsup.Controls.Add(td);

                                 td = new TableCell();
                                 td.Text = string.Format("S/.{0:N2}", lista.Sum(p =>p.hasta60mas)+ separaicion.hasta60mas);
                                 td.HorizontalAlign = HorizontalAlign.Right;
                                 td.Font.Bold = true;
                                 trsup.Controls.Add(td);




                                 resumenTBL.Controls.Add(trsup);



                                 

            
        }
    }
}